<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * class User_model
 * @author Davinder
 * @version 1.0
 */
 
class User_model extends CI_Model
{
	private $_tables;
	private $_debug_mode = FALSE;
	
	public function __construct() {
        parent::__construct();
		
		$this->_tables = new stdClass();
		$this->_tables->users = 'yb_scrapm_users';
        $this->_tables->academic_years = 'yb_scrapm_academic_years';
		$this->_tables->profiles = 'yb_scrapm_profiles';
        $this->_tables->schools = 'yb_scrapm_schools';
        
		// Load DB if its enabled
		if ( config_item('rest_database_group') )
		{
			$this->db = $this->load->database(config_item('rest_database_group'), TRUE);
		}
	}
	
	private function _user_already_exist($p_facebook_idn=null) {
	
		if ( is_null($p_facebook_idn) ) {
			return FALSE;
		}
		
		$this->db->where('facebook_idn',$p_facebook_idn);
		$this->db->where('is_active','1');
		$query = $this->db->get($this->_tables->users);
		
		if ( $this->_debug_mode == TRUE ) {
			log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
		}
		
		if ( $query->num_rows() > 0 ) {
			
			$row = $query->result();
			return $row[0]->id;
		} else {
			return FALSE;
		}
	}
    
	public function save_user_login_data($p_data = null) {
	
		if ( is_object($p_data) ) {
			
			// it returns user id if user already exists otherwise FALSE;
			$user_exists = $this->_user_already_exist($p_data->facebook_idn);
			
			if ( $user_exists === FALSE ) {
			
				$data = new stdClass();
			
				$data->firstname = $p_data->firstname;
				$data->lastname = $p_data->lastname;
				$data->email_address = $p_data->email;
				$data->dob = $p_data->dob;
				$data->gender = $p_data->gender;
				$data->facebook_idn = $p_data->facebook_idn;
				$data->password = null;
				$data->date_updated = null;
                $data->profile_image_url = null;
				$data->is_active = 1;
				
				$this->db->set('date_added','now()',FALSE);
				$query_status = $this->db->insert($this->_tables->users,$data);
				
				if ( $this->_debug_mode == TRUE ) {
                    log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
				}
		
				unset($data);
				
				if ( $query_status == TRUE ) {
                    
                    $response = new stdClass();
                    
                    $response->user_id = $this->db->insert_id();
                    
                    $data = new stdClass();
                
                    $data->profile_user_id = $response->user_id;
                    $data->profile_school_id = 0;
                    $data->profile_academic_year = 1;
                    $data->profile_class = 0;
                    
                    $this->db->set('profile_create_date','now()',FALSE);
                    $this->db->insert($this->_tables->profiles,$data);
                    
                    if ( $this->_debug_mode == TRUE ) {
                        log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
                    }
                    
                    $response->returning_user = false;
                    
					return $response;
				} else {
					return FALSE;
				}
			} else {
                $response = new stdClass();
                    
                $response->user_id = $user_exists;
                $response->returning_user = true;
                
				return $response;
			}
		} else {
			return FALSE;
		}
	}
    
    public function move_and_update_picture($p_data) {
        
        $data = new stdClass();
        
        $this->db->set('date_updated','now()',FALSE);
        
        $data->profile_thumbnail = $p_data->thumbnail;
        $data->profile_mainimage = $p_data->fullimage;
        $data->flag_is_profile_image_facebook = 0;
        
        $this->db->where('id',$p_data->user_id);
        $query_status = $this->db->update($this->_tables->users,$data);
        
        if ($query_status) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	public function update_user_login_data($p_data = null) {
	
		if ( is_object($p_data) ) {
			
			// it returns user id if user already exists otherwise FALSE;
			$user_exists = $this->_user_already_exist($p_data->facebook_idn);
			
			if ( $user_exists == TRUE ) {
			
				$data = new stdClass();
			
				$user_id = $p_data->user_id;
				$facebook_idn = $p_data->facebook_idn;
				
                if ( !empty($p_data->firstname) ) {
                    $data->firstname = $p_data->firstname;
                }
                
                if ( !empty($p_data->lastname) ) {
                    $data->lastname = $p_data->lastname;
                }
                
                if ( !empty($p_data->email) ) {
                    $data->email_address = $p_data->email;
                }
                
                if ( !empty($p_data->dob) ) {
                    $data->dob = $p_data->dob;
                }
                
                if ( !empty($p_data->gender) ) {
                    $data->gender = $p_data->gender;
                }
                
                if ( $p_data->flag_is_profile_image_facebook == 1 && !empty($p_data->profile_img) ) {
                    $data->profile_image_url = $p_data->profile_img;
                }
                
                $data->flag_is_profile_image_facebook = $p_data->flag_is_profile_image_facebook;
                
                $this->db->set('date_updated','now()',FALSE);
				$this->db->where('id',$user_id);
				$query_status = $this->db->update($this->_tables->users,$data);
				
				if ( $this->_debug_mode == TRUE ) {
                    log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
				}
		
				unset($data);
				
				if ( $query_status == TRUE ) {
                    
                    $data = new stdClass();
                
                    if ( !empty($p_data->school_id) ) {
                        $data->profile_school_id = $p_data->school_id;
                    }
                    
                    if ( !empty($p_data->academic_year) ) {
                        $data->profile_academic_year = $p_data->academic_year;
                    }
                    
                    if ( !empty($p_data->school_class) ) {
                        $data->profile_class = $p_data->school_class;
                    }
                    
                    $data->profile_status = 'VERIFIED';
                    
                    $this->db->set('profile_update_date','now()',FALSE);
                    $this->db->where('profile_user_id',$user_id);
                    $this->db->update($this->_tables->profiles,$data);
                    
                    if ( $this->_debug_mode == TRUE ) {
                        log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
                    }
                    
                    $response = new stdClass();
                    
                    $response->user_id = $user_id;
                    $response->returning_user = false;
                    
					return $response;
				} else {
					return FALSE;
				}
			} else {
                return FALSE;
			}
		} else {
			return FALSE;
		}
	}
	
	public function update_user_profile_image($p_data = null) {
	
		if ( is_object($p_data) ) {
			
			// it returns user id if user already exists otherwise FALSE;
			$user_exists = $this->_user_already_exist($p_data->facebook_idn);
			
			if ( $user_exists == TRUE ) {
			
				$data = new stdClass();
			
				$user_id = $p_data->user_id;
				$facebook_idn = $p_data->facebook_idn;
				
				$data->profile_image_url = $p_data->profile_img;
				
				$this->db->set('date_updated','now()',FALSE);
				$this->db->where('id',$user_id);
				$query_status = $this->db->update($this->_tables->users,$data);
				
				if ( $this->_debug_mode == TRUE ) {
                    log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
				}
		
				unset($data);
				
				if ( $query_status == TRUE ) {
                    return TRUE;
				} else {
					return FALSE;
				}
			} else {
                return FALSE;
			}
		} else {
			return FALSE;
		}
	}
	
	public function get_user_profile_data($p_data=null) {
		
		if ( is_object($p_data) ) {
            
            $this->db->select($this->_tables->users.'.*, '.$this->_tables->profiles.'.*,'.$this->_tables->academic_years.'.*, '.$this->_tables->schools.'.school_name');
			$this->db->where('id',$p_data->id);
			$this->db->where('facebook_idn',$p_data->facebook_idn);
			$this->db->where('is_active','1');
			$this->db->from($this->_tables->users);
            $this->db->join($this->_tables->profiles,'id = profile_user_id','LEFT');
            $this->db->join($this->_tables->academic_years,'profile_academic_year = academic_id','LEFT');
            $this->db->join($this->_tables->schools,'profile_school_id = school_id','LEFT');
			$query = $this->db->get();
			
			if ( $this->_debug_mode == TRUE ) {
				log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
			}
	
			if ( $query->num_rows() > 0 ) {
			
				$row = $query->result();
                unset($row[0]->password);
				return $row[0];
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}
	}
    
    public function get_active_academic_year() {
        
        $this->db->where('academic_is_current','1');
        $query = $this->db->get($this->_tables->academic_years);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
        
            $row = $query->result();
            
            $data = new stdClass();
            
            $data->academic_id = $row[0]->academic_id;
            $data->academic_name = $row[0]->academic_name;
            
            return $data;
        } else {
            return FALSE;
        }
    }


    public function update_user_status($p_data = null) {

        if ( is_object($p_data) ) {

            $data = new stdClass();

            $user_id = $p_data->user_id;
            $data->is_active = $p_data->is_active;
            if ($data->is_active == 0) {
                $data->deactivate_reason = $p_data->reason;
            }

            $this->db->set('date_updated','now()',FALSE);
            $this->db->where('id',$user_id);
            $query_status = $this->db->update($this->_tables->users,$data);


            if ( $query_status == TRUE ) {
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }
    
    public function get_user_profile_data_by_userid($p_data=null) {
		
		if ( is_object($p_data) ) {
            
            $this->db->select($this->_tables->users.'.*, '.$this->_tables->profiles.'.*,'.$this->_tables->academic_years.'.*, '.$this->_tables->schools.'.school_name');
			$this->db->where('id',$p_data->user_id);
			$this->db->from($this->_tables->users);
            $this->db->join($this->_tables->profiles,'id = profile_user_id','LEFT');
            $this->db->join($this->_tables->academic_years,'profile_academic_year = academic_id','LEFT');
            $this->db->join($this->_tables->schools,'profile_school_id = school_id','LEFT');
			$query = $this->db->get();
			
			if ( $this->_debug_mode == TRUE ) {
				log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
			}
	
			if ( $query->num_rows() > 0 ) {
			
				$row = $query->result();
                unset($row[0]->password);
				return $row[0];
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}
	}
    
    public function get_selfies_details($p_data = null) {
        
        if ( is_object($p_data) ) {
            
            $this->db->select($this->_tables->users.'.*, '.$this->_tables->profiles.'.*');
			$this->db->where('profile_school_id',$p_data->school_id);
			$this->db->where('academic_id',$p_data->academic_id);
			////$this->db->where('class_id','1');
			$this->db->from($this->_tables->users);
            $this->db->join($this->_tables->profiles,'id = profile_user_id','LEFT');
            $this->db->join($this->_tables->academic_years,'profile_academic_year = academic_id','LEFT');
            $this->db->join($this->_tables->schools,'profile_school_id = school_id','LEFT');
			$query = $this->db->get();
			
			if ( $this->_debug_mode == TRUE ) {
				log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
			}
	
			if ( $query->num_rows() > 0 ) {
			
                $resultSet = $query->result();
                
                $i = 0;
                
                foreach ( $resultSet as $row ) {
                    
                    $data['resultSet'][$i] = new stdClass();
                    unset($row->facebook_idn);
                    unset($row->date_added);
                    unset($row->date_updated);
                    unset($row->flag_is_profile_image_facebook);
                    unset($row->is_active);
                    unset($row->profile_id);
                    unset($row->profile_user_id);
                    unset($row->profile_school_id);
                    unset($row->profile_academic_year);
                    unset($row->profile_status);
                    unset($row->profile_create_date);
                    unset($row->profile_update_date);
                    unset($row->password);
                    $data['resultSet'][$i] = $row;
                    
                    $i++;
                }
				
				return $data;
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}        
    }
}